﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace homework7
{
    internal class Program
    {
        static void Main(string[] args)
        {
            
            OrderService orderService = new OrderService();
            //创建第一个订单
            Order order1 = new Order 
            { OrderId = 1, 
              Customer = "Customer1", 
              Details = new List<OrderDetails> 
              { new OrderDetails 
              { ProductName = "Product1", 
                  Quantity = 2, 
                  UnitPrice = 10 
              } 
              } 
            };
            //将第一个订单加入队列
            orderService.AddOrder(order1);

            //相同订单不能重复添加测试：
            //orderService.AddOrder(order1);

            //创建第二个订单
            Order order2 = new Order 
            { OrderId = 2,
              Customer = "Customer2", 
              Details = new List<OrderDetails> 
              { new OrderDetails 
              { ProductName = "Product2", 
                  Quantity = 3, 
                  UnitPrice = 20 
              } 
              } 
            };
            //将第二个订单加入队列
            orderService.AddOrder(order2);

            //创建第三个订单
            Order order3 = new Order
            {
                OrderId = 3,
                Customer = "Customer3",
                Details = new List<OrderDetails>
              { new OrderDetails
              { ProductName = "Product3",
                  Quantity = 4,
                  UnitPrice = 5
              }
              }
            };
            //将第三个订单加入队列
            orderService.AddOrder(order3);

            //创建第四个订单
            Order order4 = new Order
            {
                OrderId = 4,
                Customer = "Customer1",
                Details = new List<OrderDetails>
              { new OrderDetails
              { ProductName = "Product4",
                  Quantity = 2,
                  UnitPrice = 5
              }
              }
            };
            //将第四个订单加入队列
            orderService.AddOrder(order4);

            //查询第一个顾客的订单（查询成功，按照总价升序排列）
            List<Order> result = orderService.QueryOrders(order => order.Customer == "Customer1");
            Console.WriteLine("Query result:");
            foreach (Order order in result)
            {
                Console.WriteLine(order);
            }
            //查询订单号为1的订单
            List<Order> result2 = orderService.QueryOrders(order => order.OrderId == 1);
            Console.WriteLine("Query result:");
            foreach (Order order in result2)
            {
                Console.WriteLine(order);
            }
            //查询订单总价为60的订单
            List<Order> result3 = orderService.QueryOrders(order => order.TotalAmount==60 );
            Console.WriteLine("Query result:");
            foreach (Order order in result3)
            {
                Console.WriteLine(order);
            }

            //删除第二个订单
            try
            {
                orderService.RemoveOrder(2);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
            //查询第二个订单（查询失败）
            List<Order> result4 = orderService.QueryOrders(order => order.Customer == "Customer2");
            Console.WriteLine("Query result:");
            foreach (Order order in result4)
            {
                Console.WriteLine(order);
            }
            //对订单排序（剩下1号，3号，4号订单）
            orderService.SortOrders(order => order.Customer);
            Console.WriteLine("Sorted orders:");
            foreach (Order order in orderService.QueryOrders(order => true))
            {
                Console.WriteLine(order);
            }
            Console.ReadKey();
        }
    }
    class OrderDetails
    {
        public string ProductName { get; set; }//订单产品名称
        public int Quantity { get; set; }//订单数量
        public double UnitPrice { get; set; }//订单单价
        public override bool Equals(object obj)//重写equals方法
        {
            if (!(obj is OrderDetails)) return false;
            OrderDetails other = (OrderDetails)obj;
            return this.ProductName == other.ProductName;
        }
        public override int GetHashCode()//重写gethashcode函数
        {
            return ProductName.GetHashCode();
        }
        public override string ToString()//重写tostring方法改为显示订单详细信息
        {
            return $"Product: {ProductName}, Quantity: {Quantity}, Unit Price: {UnitPrice}";
        }
    }
    class Order
    {
        public int OrderId { get; set; }//订单号
        public string Customer { get; set; }//订单客户
        public List<OrderDetails> Details { get; set; }
        public double TotalAmount => Details.Sum(detail => detail.Quantity * detail.UnitPrice);//计算订单的总金额
        public override bool Equals(object obj)
        {
            if (!(obj is Order)) return false;
            Order other = (Order)obj;
            return this.OrderId == other.OrderId;
        }
        public override int GetHashCode()
        {
            return OrderId.GetHashCode();
        }
        public override string ToString()
        {
            return $"Order  ID:{OrderId},Customer:{Customer},Total Amount:{TotalAmount}";
        }
    }
    class OrderService
    {
        private List<Order> orders = new List<Order>();
        public void AddOrder(Order order)//像list添加订单
        {
            if (orders.Contains(order))
                throw new InvalidCastException("Order already exist");
            orders.Add(order);
        }
        public void RemoveOrder(int orderId)//根据订单号从list删除订单
        {
            Order order = orders.FirstOrDefault(o => o.OrderId == orderId);
            if (order == null)
                throw new KeyNotFoundException("order not found");
            orders.Remove(order);
        }
        public List<Order>QueryOrders(Func<Order,bool>predicate)//根据具体查询条件查询订单
        {
            return orders
                   .Where(predicate)
                   .OrderBy(order => order.TotalAmount)
                   .ToList();
        }
        public void SortOrders(Func<Order,object>KeySelector)//根据主要排序条件对订单进行排序
        {
            orders = orders
                   .OrderBy(KeySelector)
                   .ToList();
        }
    }
}
